package com.buss.admin.controller;

import java.util.List;

import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.admin.entity.AuthFunctionEntity;
import com.buss.admin.entity.AuthRoleEntity;
import com.buss.admin.entity.RoleFunctionEntity;
import com.buss.admin.service.IAuthFunctionService;
import com.buss.admin.service.IRoleFunctionService;
import com.buss.admin.service.IRoleService;
import com.buss.admin.trans.form.RoleForm;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;
import com.buss.common.util.IDUtils;

@RequestMapping("/admin/role")
@Controller
public class RoleController {

	@Autowired
	private IRoleService roleService;

	@Autowired
	private IAuthFunctionService functionService;

	@Autowired
	private IRoleFunctionService roleFunctionService;

	@RequestMapping("/getAllRoles")
	@ResponseBody
	public List<AuthRoleEntity> getAllRoles() {

		List<AuthRoleEntity> list = roleService.getList(AuthRoleEntity.class);
		return list;

	}

	@RequestMapping("/list")
	@ResponseBody
	public EasyUIDataGridResult getRoleList(RoleForm roleForm) {

		// 分页查询
		CriteriaQuery cq = roleForm.getCriteriaQuery();

		/*
		 * 设置查询条件
		 */
		// cq.add(Restrictions.sizeGe("checkstatus", 10));
		// cq.add(Restrictions.ge("checkstatus", 10));

		int pageNo = Integer.parseInt(roleForm.getPage());
		int pageSize = Integer.parseInt(roleForm.getRows());

		// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		PageMode<AuthRoleEntity> pageMode = roleService.list4page(cq, pageNo, pageSize);

		// 封装数据
		result.setRows(pageMode.getResult());
		result.setTotal(pageMode.getTotal());

		return result;

	}

	@RequestMapping("/saveorupdate")
	@ResponseBody
	public HXResult SaveOrUpdate(AuthRoleEntity roleEntity, String ids) {

		try {

			String id = roleEntity.getId();
			// 执行添加操作
			if (id == null || "".equals(id)) {

				// 保存实体对象
				String id2 = IDUtils.getNumStringId();
				roleEntity.setId(id2);
				roleService.saveOrUpdate(roleEntity);

				// 关联外键关系
				// 权限id
				if (!"".equals(ids)) {
					String[] idStrings = ids.split(",");
					for (String string : idStrings) {
						RoleFunctionEntity roleFunctionEntity = new RoleFunctionEntity();
						// 获取权限实体
						AuthFunctionEntity functionEntity = functionService.get(AuthFunctionEntity.class, string);
						String numStringId = IDUtils.getNumStringId();

						roleFunctionEntity.setId(numStringId);
						roleFunctionEntity.setRole(roleEntity);
						// roleFunctionEntity.setFunction(functionEntity);
						roleFunctionEntity.setFunctionEntity(functionEntity);

						roleFunctionService.saveOrUpdate(roleFunctionEntity);

					}

				}

				return HXResult.ok("操作成功！");

			} else {
				// 执行修改操作

				// 删除外键关联
				roleFunctionService.executeSql("DELETE FROM	hx_role_function WHERE	role_id = ?", roleEntity.getId());

				roleService.saveOrUpdate(roleEntity);

				// 关联外键关系
				// 权限id
				if (!"".equals(ids)) {
					String[] idStrings = ids.split(",");
					for (String string : idStrings) {
						RoleFunctionEntity roleFunctionEntity = new RoleFunctionEntity();
						// 获取权限实体
						AuthFunctionEntity functionEntity = functionService.get(AuthFunctionEntity.class, string);
						String numStringId = IDUtils.getNumStringId();

						roleFunctionEntity.setId(numStringId);
						roleFunctionEntity.setRole(roleEntity);
						// roleFunctionEntity.setFunction(functionEntity);
						roleFunctionEntity.setFunctionEntity(functionEntity);

						roleFunctionService.saveOrUpdate(roleFunctionEntity);

					}
				}

				return HXResult.ok("操作成功!");

			}

		} catch (Exception e) {
			e.printStackTrace();

			return HXResult.ok("服务器繁忙，操作数据失败！，请稍后再试！");
		}

	}

	@RequestMapping("/delete")
	@ResponseBody
	public HXResult deleteRole(String ids) {

		try {
			String[] strings = ids.split(",");

			for (String id : strings) {
				// 清除外键关系
				// 删除外键关联
				roleFunctionService.executeSql("DELETE FROM	hx_role_function WHERE	role_id = ?", id);
				roleFunctionService.executeSql("DELETE FROM	hx_user_role WHERE	role_id = ?", id);
				roleFunctionService.deleteEntityById(AuthRoleEntity.class, id);

			}

			return HXResult.ok("操作数据成功！");

		} catch (Exception e) {
			e.printStackTrace();

			return HXResult.build(500, "服务器繁忙，请稍候再试！");

		}

	}

	/*
	 * 角色相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		System.out.println("path:" + path);
		return "admin/role/" + path;
	}

}
